/**
 * author:Administrator
 * date:2016/3/14
 * desc: 整体 command 流程
 */

'use strict';

let assert = require('assert');

let cmdService = require('../application/cmdService.js');
let queryService = require('../application/queryService.js');
let roleService = require('../application/roleService.js');


describe('cmdService', function () {
  it('category', function*() {
    let cate = {name: 'cate1', pid: 0};
    let cateX = yield cmdService.createCategory(cate.name);
    assert.equal(true, !!cateX);
    cateX = yield queryService.oneCategory(cateX.id);
    assert.equal(true, !!cateX);
    assert.equal(cateX.name, cate.name);
    //
    yield cmdService.destroyCategory(cateX.id, true);
  });

  it('location', function*() {
    let loc = {name: 'loc1', pid: 0};
    let locX = yield cmdService.createLocation(loc.name, loc.pid);
    assert.equal(true, !!locX);
    locX = yield queryService.oneLocation(locX.id);
    assert.equal(true, !!locX);
    assert.equal(locX.name, loc.name);
    //
    yield cmdService.destroyLocation(locX.id, true);
  });
});

describe('roleService', function () {
  it('createUser', function*() {
    // create user
    let user = {name: 'wzq', pwd: 'wzq'};
    yield roleService.registerUser(user.name, user.pwd);
    let userX = yield roleService.login(user.name, user.pwd);
    assert.equal(true, !!userX);
    assert.equal(userX.name, user.name);

    /** create role customer */
    let cust = {name: 'cust', uId: userX.id};
    yield roleService.registerCustomer(cust);

    /** create role mgr */
    let mgr = {name: 'mgr', uId: userX.id};
    yield roleService.registerManager(mgr);

    /** create role vendor */
    let vd = {name: 'vd', uId: userX.id};
    yield roleService.registerVendor(vd);

    /** create role keeper */
    let kpr = {name: 'kpr', uId: userX.id};
    yield roleService.registerKeeper(kpr);

    let allRoles = yield queryService.allUserRole(userX.id);
    // console.log('allRoles', allRoles);
    assert.equal(true, !!allRoles);
    //
    assert.equal(true, !!allRoles.customer);
    assert.equal(cust.name, allRoles.customer[0].name);
    //
    assert.equal(true, !!allRoles.manager);
    assert.equal(mgr.name, allRoles.manager[0].name);
    //
    assert.equal(true, !!allRoles.vendor);
    assert.equal(vd.name, allRoles.vendor[0].name);
    //
    assert.equal(true, !!allRoles.shopkeeper);
    assert.equal(kpr.name, allRoles.shopkeeper[0].name);

    // drop user
    yield roleService.destroyUser(userX.id, true);
    //
    // drop roles
    yield roleService.destroyCustomer(allRoles.customer[0].id, true);
    yield roleService.destroyVendor(allRoles.vendor[0].id, true);
    yield roleService.destroyManager(allRoles.manager[0].id, true);
    yield roleService.destroyShopkeeper(allRoles.shopkeeper[0].id, true);
  });
});